﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml.Linq;
using deezer.sdk.windowsphone.Helpers;

namespace deezer.sdk.windowsphone
{
    public delegate void GetEditorialResponseEventHandler(object sender, EventArgsEditorial e);

    public class Editorial
    {
        HttpWebRequest request;

        public event GetEditorialResponseEventHandler GetEditorialResponse;

        protected virtual void GetEditorialResponseCheck(EventArgsEditorial e)
        {
            if (GetEditorialResponse != null)
                GetEditorialResponse(this, e);
        }

        public void GetEditorialAsync(Int32 id)
        {
            Uri uriId = new Uri(@"http://api.deezer.com/2.0/editorial&output=xml");
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseIntern), null);
        }

        private void ByIdResponseIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsEditorial e = new EventArgsEditorial(Converter.ConvertJsonToTypeEditorial(responseStream.ReadToEnd()));
                        GetEditorialResponseCheck(e);
                    }
                }
            }
        }
    }
}
